package com.shop.controller;

import com.shop.constant.Constants;
import com.shop.entity.Order;
import com.shop.entity.UserOrderList;
import com.shop.exception.DAOException;
import com.shop.factory.ObjectFactory;
import com.shop.service.UserOrderServicec;
import com.shop.util.GetMap;
import com.shop.util.PrintJson;
import com.shop.util.ReturnImg;
import com.sun.org.apache.xpath.internal.operations.Or;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/userOrder/*")
public class UserOrderController extends HttpServlet {
    UserOrderServicec service = (UserOrderServicec) ObjectFactory.getInstance(Constants.USEORDERRSERVICE_KEY);


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        if("/shop/userOrder/getAllOrderList".equals(uri)) {
            getAllOrderList(req,resp);
        }else if ("/shop/userOrder/userUpOrder".equalsIgnoreCase(uri)){
            userUpOrder(req,resp);
        }else if ("/shop/userOrder/getAllOrderDetialList".equalsIgnoreCase(uri)){
            getAllOrderDetialList(req,resp);
        }else if ("/shop/userOrder/getAllOrderListLike".equalsIgnoreCase(uri)){
            getAllOrderListLike(req,resp);
        } else if ("/shop/userOrder/getOrderDetailByOid".equalsIgnoreCase(uri)) {
            //????????????????????
            getOrderDetailByOid(req,resp);
        }else if ("/shop/userOrder/getOrderDetailByOidMoney".equalsIgnoreCase(uri)){
            getOrderAllMoney(req,resp);
        }else if ("/shop/userOrder/getUserOrderByUidAndOid".equalsIgnoreCase(uri)){
            //??????????????
            getUserOrderByUidAndOid(req,resp);
        }else if ("/shop/userOrder/getUserOrderAndDetial".equalsIgnoreCase(uri)){
            //???????????????????????????????
            getUserOrderAndDetial(req,resp);
        }else if ("/shop/userOrder/orderDelect".equalsIgnoreCase(uri)){
            //??????????
            delectOrder(req,resp);
        }else if ("/shop/userOrder/getImg".equalsIgnoreCase(uri)){

            getImg(req,resp);
        }

    }

    private void getImg(HttpServletRequest req, HttpServletResponse resp) {
        String gd_img = req.getParameter("gd_img");
        System.out.println(gd_img);
        Map<String, Object> map = null;
        try {
            ReturnImg.returnImg(resp,gd_img);
            map = GetMap.getMap(200, "????????");
//            PrintJson.printJsonObj(resp,map);
        } catch (DAOException e) {
            e.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
    }

    private void delectOrder(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("???");
        int uid = Integer.parseInt(req.getParameter("uid"));
        int oid = Integer.parseInt(req.getParameter("oid"));
        System.out.println("oid" +oid);
        Map<String,Object> map = null;
        try {
            service.delectOrder(uid,oid);
            map = GetMap.getMap(200, "?????????????");
        } catch (DAOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        PrintJson.printJsonObj(resp,map);
//        Map<String, Object> map = null;
//        try {
//            map = GetMap.getMap(200, "?????????????");
//            PrintJson.printJsonObj(resp,map);
//        } catch (DAOException | SQLException e) {
//            e.printStackTrace();
//        }
//        map.put("data",map);
//        PrintJson.printJsonObj(resp,map);
    }

    private void getUserOrderAndDetial(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int u_id = Integer.parseInt(req.getParameter("u_id"));
        String o_pay = req.getParameter("o_pay");
        String o_status = req.getParameter("o_status");
        String g_status = req.getParameter("g_status");
        List<Order> order = service.getUserOrderAndDetial(u_id,o_pay,o_status,g_status);
        Map<String, Object> map = null;
        map = GetMap.getMap(200, "?????????????????????");
        map.put("data",order);
        PrintJson.printJsonObj(resp,map);
    }

    private void getUserOrderByUidAndOid(HttpServletRequest req, HttpServletResponse resp) {
        int u_id = Integer.parseInt(req.getParameter("u_id"));
        int o_id = Integer.parseInt(req.getParameter("o_id"));
        Order order = service.getUserOrderByUidAndOid(u_id,o_id);
        Map<String, Object> map = null;
        map = GetMap.getMap(200, "??????????????");
        map.put("data",order);
        PrintJson.printJsonObj(resp,map);
    }

    private void getOrderAllMoney(HttpServletRequest req, HttpServletResponse resp) {
        int u_id = Integer.parseInt(req.getParameter("u_id"));
        int o_id = Integer.parseInt(req.getParameter("o_id"));
        Map<String, Object> map = null;
        List<UserOrderList> lists = service.getOrderDetailByOid(u_id,o_id);
        double total = 0;
        for (UserOrderList userOrder:lists){
            double price = Double.parseDouble(userOrder.getBank1());
            total +=price;
        }
        map = GetMap.getMap(200, "?????????????????");
        map.put("data",total);
        PrintJson.printJsonObj(resp,map);
    }

    private void getOrderDetailByOid(HttpServletRequest req, HttpServletResponse resp) {
        int u_id = Integer.parseInt(req.getParameter("u_id"));
        int o_id = Integer.parseInt(req.getParameter("o_id"));
        Map<String, Object> map = null;
        List<UserOrderList> lists = service.getOrderDetailByOid(u_id,o_id);
        map = GetMap.getMap(200, "??????????id????????????");
        map.put("data",lists);
        PrintJson.printJsonObj(resp,map);
    }

    private void userUpOrder(HttpServletRequest req, HttpServletResponse resp) {
        int u_id = Integer.parseInt(req.getParameter("u_id"));
        int o_id = Integer.parseInt(req.getParameter("o_id"));
        int o_status = Integer.parseInt(req.getParameter("o_status"));
        Map<String, Object> map = null;
        service.userUpOrder(u_id,o_id,o_status);
        map = GetMap.getMap(200, "????????????");
        PrintJson.printJsonObj(resp,map);
    }

    private void getAllOrderDetialList(HttpServletRequest req, HttpServletResponse resp) {
        int u_id  = Integer.parseInt(req.getParameter("u_id"));
        String od_status = req.getParameter("od_status");
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Map<String, Object> map = null;
        List<UserOrderList> lists = null;
        lists = service.getAllOrderDetialList(u_id,od_status,pageNum, pageSize);
        System.out.println(lists);
        map = GetMap.getMap(200, "????????????????????????????");
        map.put("data",lists);
        PrintJson.printJsonObj(resp,map);
    }


    private void getAllOrderList(HttpServletRequest req, HttpServletResponse resp) {
        int uid = Integer.parseInt(req.getParameter("u_id"));
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Map<String, Object> map = null;
        List<Order> lists = null;
        lists = service.getAllUserOrder(uid,pageNum,pageSize);
        map = GetMap.getMap(200, "???????????????");
        map.put("data",lists);
        PrintJson.printJsonObj(resp,map);
    }



    private void getAllOrderListLike(HttpServletRequest req, HttpServletResponse resp) {
        int uid = Integer.parseInt(req.getParameter("u_id"));
        String collection = req.getParameter("collection");
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Map<String, Object> map = null;
        List<Order> lists = null;
        lists = service.getAllUserOrderLike(uid,collection,pageNum,pageSize);
        map = GetMap.getMap(200, "???????????????");
        map.put("data",lists);
        PrintJson.printJsonObj(resp,map);
    }

}
